package fr.charlott.core.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import fr.charlott.core.repository.AdresseJpaRepository;
import fr.charlott.core.repository.ClienteJpaRepository;
import fr.charlott.core.repository.HotesseJpaRepository;
import fr.charlott.core.repository.model.Cliente;
import fr.charlott.core.repository.model.Hotesse;
import fr.charlott.core.service.ClienteService;

@Service("coreClienteService")
public class ClienteServiceImpl implements ClienteService {

    private ClienteJpaRepository clienteJpaRepository;

    private HotesseJpaRepository hotesseJpaRepository;

    private AdresseJpaRepository adresseJpaRepository;

    public ClienteServiceImpl() {
        super();
    }

    @Autowired
    public ClienteServiceImpl(ClienteJpaRepository clienteJpaRepository, HotesseJpaRepository hotesseJpaRepository, AdresseJpaRepository adresseJpaRepository) {
        this();
        this.clienteJpaRepository = clienteJpaRepository;
        this.hotesseJpaRepository = hotesseJpaRepository;
        this.adresseJpaRepository = adresseJpaRepository;
    }

    @Override
    @Transactional
    public void ajouterCliente(Cliente cliente) {
        Cliente clienteExist = chercherParNomPrenom(cliente.getNom(), cliente.getPrenom());
        Hotesse hotesseExist = hotesseJpaRepository.findByNomAndPrenomAllIgnoreCase(cliente.getNom(), cliente.getPrenom());

        if (hotesseExist == null) {
            // Update cliente
            if (clienteExist != null) {
                clienteExist.setAchats(cliente.getAchats());
                adresseJpaRepository.delete(clienteExist.getAdresse());
                clienteExist.setAdresse(cliente.getAdresse());
                clienteExist.setCommentaires(cliente.getCommentaires());
                clienteExist.setDateDeNaissance(cliente.getDateDeNaissance());
                clienteExist.setDerniereVisite(cliente.getDerniereVisite());
                clienteExist.setHotesse(cliente.getHotesse());
                clienteExist.setIndividuelle(cliente.isIndividuelle());
                clienteExist.setPreferences(cliente.getPreferences());
                clienteExist.setProduits(cliente.getProduits());
                clienteExist.setTailleBas(cliente.getTailleBas());
                clienteExist.setTailleHaut(cliente.getTailleHaut());
                clienteJpaRepository.saveAndFlush(clienteExist);
            } else {
                // Insert cliente
                clienteJpaRepository.saveAndFlush(cliente);
            }
        }
    }

    @Override
    @Transactional
    public void supprimerCliente(Cliente cliente) {
        clienteJpaRepository.delete(cliente);
    }

    @Override
    @Transactional
    public void supprimerToutesLesClientes() {
        clienteJpaRepository.deleteAll();
    }

    @Override
    public Cliente chercherParId(Long id) {
        return clienteJpaRepository.findOne(id);
    }

    @Override
    @Transactional(readOnly = true)
    public Cliente chercherParNomPrenom(String nom, String prenom) {
        return clienteJpaRepository.findByNomAndPrenomAllIgnoreCase(nom, prenom);
    }

    @Override
    @Transactional(readOnly = true)
    public List<Cliente> chercherParHotesse(Hotesse hotesse) {
        return clienteJpaRepository.findByHotesse(hotesse);
    }

    @Override
    @Transactional(readOnly = true)
    public List<Cliente> chargerToutesLesClientes() {
        return clienteJpaRepository.findAll();
    }

    // getters - setters

    /**
     * @param clienteJpaRepository
     *            the clienteJpaRepository to set
     */
    public void setClienteClienteJpaRepository(ClienteJpaRepository clienteJpaRepository) {
        this.clienteJpaRepository = clienteJpaRepository;
    }

}
